package com.ll.controller;
import com.ll.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;

/**
 * 2020/9/12 - 19:06
 * 常用的注解
 */
@Controller
@RequestMapping("/anno")

//只能作用于类上
@SessionAttributes(value = "msg") //把msg=刘龙存入到session域中
public class AnnoControl {

    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(name = "testname") String username) {
        System.out.println("执行了AnnoControl => testRequestParam");
        System.out.println(username);
        return "success";
    }

    /**
     * 获取到请求体的内容,get请求不适用,一般都是post发送
     * @return
     */
    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String body) {
        System.out.println("执行了AnnoControl => testRequestBody");
        String decode = null;
        try {
            decode = URLDecoder.decode(body, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.out.println("testRequestBody异常");
        }
        System.out.println(decode);
        return "success";
    }

    /**
     * @PathVariable
     */
    /*anno.jsp,对应的方法是get,方法添加,method = RequestMethod.PUT, 服务器会报405错误*/
    @RequestMapping(value = "/testPathVariable/{sid}" /*, method = RequestMethod.PUT*/)
    public String testPathVariable(@PathVariable("sid") String id) {
        System.out.println("执行了AnnoControl => testPathVariable");
        System.out.println(id);
        return "success";
    }

    /**
     * 获取请求头
     * @param header
     * @return
     */
    @RequestMapping(value = "/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value = "Accept") String header) {
        System.out.println("执行了AnnoControl => testRequestHeader");
        System.out.println(header);
        return "success";
    }

    /**
     * 获取Cookie的值
     * @return
     */
    @RequestMapping(value = "/testCookieValue")
    public String testCookieValue(@CookieValue(value = "JSESSIONID") String cookieValue) {
        System.out.println("执行了AnnoControl => testCookieValue");
        System.out.println(cookieValue);
        return "success";
    }

    /**
     * @ModelAttribute
     * @return
     */
    @RequestMapping(value = "/testModelAttribute")
    public String testModelAttribute(@ModelAttribute("abc") User user) {
        System.out.println("执行了AnnoControl => testModelAttribute");
        System.out.println(user);
        return "success";
    }

    @ModelAttribute
    public void showUser(String uname, Map<String,User> map) {
        System.out.println("@ModelAttributes标注的方法 => showUser执行了");
        // 通过用户名查询数据库(模拟)
        User user = new User();
        user.setUname(uname);
        user.setAge(22);
        user.setDate(new Date());
        map.put("abc",user);
    }


//    @RequestMapping(value = "/testModelAttribute")
//    public String testModelAttribute(User user) {
//        System.out.println("执行了AnnoControl => testModelAttribute");
//        System.out.println(user);
//        return "success";
//    }

    /**
     * 优先执行
     */
//    @ModelAttribute
//    public User showUser(String uname) {
//        System.out.println("showUser执行了");
//        // 通过用户名查询数据库(模拟)
//        User user = new User();
//        user.setUname(uname);
//        user.setAge(22);
//        user.setDate(new Date());
//        return user;
//    }

    /**
     * @SessionAttribute的注解
     * @return
     */
    @RequestMapping(value = "/testSessionAttributes")
    public String testSessionAttributes(Model model) {
        System.out.println("执行了AnnoControl => testSessionAttributes");
        // 底层会存储到request域对象中
        model.addAttribute("msg", "刘龙");
        return "success";
    }

    @RequestMapping(value = "/getSessionAttributes")
    public String getSessionAttributes(ModelMap modelMap) {
        System.out.println("执行了AnnoControl => getSessionAttributes");
        // 底层会存储到request域对象中
        String msg = (String)modelMap.get("msg");
        System.out.println(msg);
        return "success";
    }

    /**
     * 清除
     * @param status
     * @return
     */
    @RequestMapping(value = "/delSessionAttributes")
    public String delSessionAttributes(SessionStatus status) {
        System.out.println("执行了AnnoControl => delSessionAttributes");
        status.setComplete();
        return "success";
    }



}
